package cn.mw.cmdb.processor;

import cn.mw.cmdb.entity.ModelInfo;
import cn.mw.cmdb.entity.ModelType;
import cn.mw.cmdb.entity.PropertyTypeString;
import cn.mw.cmdb.param.InstanceSearchParam;
import cn.mw.cmdb.param.TableSearchParam;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.components.mongodb.entity.SubCondition;
import cn.mw.microMonitorCommon.api.cmdb.TableIdEnum;
import cn.mw.microMonitorCommon.entity.cmdb.RelatedDataType;
import cn.mw.microMonitorCommon.entity.cmdb.TableHeaderItem;
import cn.mw.microMonitorCommon.util.SpringUtils;

import java.util.Arrays;
import java.util.List;

import static cn.mw.cmdb.enums.RelatedSelectEnum.*;
import static cn.mw.cmdb.util.CMDBTool.CMDBDefaultIdKey;
import static cn.mw.cmdb.util.CMDBTool.CMDBDefaultIdName;

public class TableDataFactory {
    public static List<String> authNameList = Arrays.asList(groupNames.getFiledName(), userNames.getFiledName(), orgNames.getFiledName());
    public static final String SEQ = "seq";
    public static String systemInterfaceConfigKey = "system.interfaceInfo.configName";

    public static String SCMONITORSYSTEMKEY = "scMonitorSystem";
    public static TableDataProccessor build(TableSearchParam param, CmdbServiceManage cmdbServiceManage ,List<String> tableHeaderSort) {
        String systemInterfaceConfig = SpringUtils.getPropertiesValue(systemInterfaceConfigKey);

        TableIdEnum tableIdEnum = TableIdEnum.getTableIdEnum(param.getTableId());
        TableDataProccessor proccessor = null;
        InstanceSearchParam instanceSearchParam = new InstanceSearchParam();
        switch (tableIdEnum) {
            case model:
                proccessor = new StaticsProcessorModel(cmdbServiceManage, param);
                break;
            case customInstance:
            case instance:
                proccessor = new StaticsProcessorMixtureInstance(cmdbServiceManage, param);
                break;
            case commonInstance:
            case assetType:
                instanceSearchParam.setGroupOrModelId(param.getConvertId());
                proccessor = new TableProcessorCommonInstance(cmdbServiceManage, param, instanceSearchParam,tableHeaderSort);
                break;
            case org:
                if (SCMONITORSYSTEMKEY.equals(systemInterfaceConfig)) {
                    instanceSearchParam.setOrgs(param.getConvertId());
                }else{
                    instanceSearchParam.setOrgIds(param.getConvertId());
                }
                proccessor = new TableProcessorCommonInstance(cmdbServiceManage, param, instanceSearchParam,tableHeaderSort);
                break;
            case brand:
                instanceSearchParam.setBrand(param.getConvertId());
                proccessor = new TableProcessorCommonInstance(cmdbServiceManage, param, instanceSearchParam,tableHeaderSort);
                break;
            case userGroup:
                instanceSearchParam.setUserGroup(param.getConvertId());
                proccessor = new TableProcessorCommonInstance(cmdbServiceManage, param, instanceSearchParam,tableHeaderSort);
                break;
            case business:
                proccessor = new StaticsProcessorBusinessInstance(cmdbServiceManage, param, instanceSearchParam, new TableProcessorCommonInstance(cmdbServiceManage, param, instanceSearchParam, tableHeaderSort));
                break;
            case custom:
                proccessor = new StaticsProcessorCustom(cmdbServiceManage, param);
                break;
            case location:
                proccessor = new StaticsProcessorLocationInstance(cmdbServiceManage, param, instanceSearchParam, new TableProcessorCommonInstance(cmdbServiceManage, param, instanceSearchParam, tableHeaderSort));
                break;
            case finance:
                proccessor = new StaticsProcessorFinanceModel(cmdbServiceManage, param);
                break;
            case financeModel:
                proccessor = new TableProcessorFinanceInstance(cmdbServiceManage, param, instanceSearchParam, tableHeaderSort);
                break;
            case businessModel:
                param.addSubCondition(CMDBTool.getFieldName(ModelInfo::getModelType)
                        , new SubCondition(CriteriaOpsType.is.name(), ModelType.BusinessModel.getType()));
                proccessor = new StaticsProcessorModel(cmdbServiceManage, param);
                break;
        }
        return proccessor;
    }

    public static TreeProccessorTable buildTree(TableSearchParam param, CmdbServiceManage cmdbServiceManage) {
        TreeProccessorTable proccessor = null;
        TableIdEnum tableIdEnum = TableIdEnum.getTableIdEnum(param.getTableId());
        switch (tableIdEnum) {
            case model -> proccessor = new TreeProcessorGroupModel(cmdbServiceManage, param);
            case org -> proccessor = new TreeProcessorOrg(cmdbServiceManage);
            case assetType -> proccessor = new TreeProcessorAssetType(cmdbServiceManage);
            case brand -> proccessor = new TreeProcessorBrand(cmdbServiceManage);
            case userGroup -> proccessor = new TreeProcessorUserGroup(cmdbServiceManage);
            case business -> proccessor = new TreeProcessorBusiness(cmdbServiceManage);
            case custom -> proccessor = new TreeProcessorCustom(cmdbServiceManage, param);
            case location -> proccessor = new TreeProcessorLocation(cmdbServiceManage);
            case parent -> proccessor = new TreeProcessorParentModel(cmdbServiceManage, param);
            case finance -> proccessor = new TreeProcessorFinanceModel(cmdbServiceManage, param);
            case businessModel -> {
                param.addSubCondition(CMDBTool.getFieldName(ModelInfo::getModelType)
                        , new SubCondition(CriteriaOpsType.is.name(), ModelType.BusinessModel.getType()));
                proccessor = new TreeProcessorGroupModel(cmdbServiceManage, param);
            }
        }
        return proccessor;
    }

    public static void createTableHeaderId(List<TableHeaderItem> tableHeaderItems){
        TableHeaderItem item = new TableHeaderItem();
        item.setField(CMDBDefaultIdKey);
        item.setHeaderName(CMDBDefaultIdName);
        item.setParseType(RelatedDataType.none);
        item.setPropertyTypeId(PropertyTypeString.ID);
        item.setVisible(false);
        if (!tableHeaderItems.contains(item)) {
            tableHeaderItems.add(0,item);
        }
    }
}
